﻿using KPI.Data;
using KPI.Entity;
using KPI.UIBase;
using ScheduleManager.DB;
using ScheduleManager.ViewModels;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using TYBase;
using TYControls;

namespace ScheduleManager.Views
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class ViewEditSchedule : TYWindow
    {
        ViewEditScheduleModel vm = null;
        public ViewEditSchedule(ScheduleVO data)
        {
            InitializeComponent();
            ObservableCollection<IntStringData> listShift = new ObservableCollection<IntStringData>();
            var dictShift = AppDictData.GetList(EnumDictType.shift);
            foreach (var item in dictShift)
            {
                IntStringData intString = new IntStringData() { Key = item.Key, Value = item.Value };
                listShift.Add(intString);
            }
            vm = new ViewEditScheduleModel(data, listShift);
            //vm.ListShift= listShift;
            this.DataContext = vm;
            this.Loaded += ViewEditSchedule_Loaded;
        }

        private async void ViewEditSchedule_Loaded(object sender, RoutedEventArgs e)
        {
            await InitDict();
        }

        private async Task InitDict()
        {
            int key1 = vm.ScheduleModel.Shift1_key.Key;
            int key2 = vm.ScheduleModel.Shift2_key.Key;
            int key3 = vm.ScheduleModel.Shift3_key.Key;
            int key4 = vm.ScheduleModel.Shift4_key.Key;
            int key5 = vm.ScheduleModel.Shift5_key.Key;
            int key6 = vm.ScheduleModel.Shift6_key.Key;
            int key7 = vm.ScheduleModel.Shift7_key.Key;

            var rData = await Run(() =>
            {
                return DictDataDBHelper.GetList($"{EnumDictType.shift}");
            });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
            AppDictData.Update(EnumDictType.shift, rData.data);


            vm.ListShift.Clear();
            var listShift = AppDictData.GetList(EnumDictType.shift);
            foreach (var item in listShift)
            {
                IntStringData intString = new IntStringData() { Key = item.Key, Value = item.Value };
                vm.ListShift.Add(intString);
            }
            if (key1 > 0) vm.ScheduleModel.Shift1_key = vm.ListShift.FirstOrDefault(x => x.Key == key1);
            if (key2 > 0) vm.ScheduleModel.Shift2_key = vm.ListShift.FirstOrDefault(x => x.Key == key2);
            if (key3 > 0) vm.ScheduleModel.Shift3_key = vm.ListShift.FirstOrDefault(x => x.Key == key3);
            if (key4 > 0) vm.ScheduleModel.Shift4_key = vm.ListShift.FirstOrDefault(x => x.Key == key4);
            if (key5 > 0) vm.ScheduleModel.Shift5_key = vm.ListShift.FirstOrDefault(x => x.Key == key5);
            if (key6 > 0) vm.ScheduleModel.Shift6_key = vm.ListShift.FirstOrDefault(x => x.Key == key6);
            if (key7 > 0) vm.ScheduleModel.Shift7_key = vm.ListShift.FirstOrDefault(x => x.Key == key7);


        }

        private async void BtnClick_Save(object sender, RoutedEventArgs e)
        {
            //保存
            if (vm.ScheduleModel == null)
            {
                this.ShowMsg("数据异常!");
                return;
            }
            Tbl_Schedule schedule = new Tbl_Schedule()
            {

                user_id = vm.ScheduleModel.data.user_id,
                start_date = vm.ScheduleModel.data.schedule.start_date,
                end_date = vm.ScheduleModel.data.schedule?.end_date,
                schedule_id = vm.ScheduleModel.data.schedule.schedule_id,
                shift1_key = vm.ScheduleModel.Shift1_key.Key,
                shift2_key = vm.ScheduleModel.Shift2_key.Key,
                shift3_key = vm.ScheduleModel.Shift3_key.Key,
                shift4_key = vm.ScheduleModel.Shift4_key.Key,
                shift5_key = vm.ScheduleModel.Shift5_key.Key,
                shift6_key = vm.ScheduleModel.Shift6_key.Key,
                shift7_key = vm.ScheduleModel.Shift7_key.Key,
            };

            var rData = await Run(() =>
            {
                return ScheduleDBHelper.Edit(schedule);
            });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
            if (rData == null)
            {
                this.ShowMsg("数据异常!");
                return;
            }
            this.DialogResult = true;
        }

        private async void BtnClick_EditShiftDict(object sender, RoutedEventArgs e)
        {
            ViewEditString view = new ViewEditString();
            if (view.ShowDialog() == false || view.Data.Null())
                return;
            string data = view.Data;
            data = data.Replace("；", "；")
                .Replace("，", ";")
                .Replace(",", ";");
            //添加字典
            var listData = data.Split(";");
            List<Tbl_DictData> queryDto = new List<Tbl_DictData>();
            foreach (var item in listData)
            {
                queryDto.Add(new Tbl_DictData
                {
                    dict_type = $"{EnumDictType.shift}",
                    dict_label = item
                });
            }
            var rData = await Run(() =>
             {
                 return DictDataDBHelper.AddList(queryDto);
             });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
            if (rData == null)
            {
                this.ShowMsg("数据异常!");
                return;
            }
            await InitDict();
        }
    }
}
